package isae.c2.android.depense.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CategoryDBAdapter {

    /** * Colonnes de la base de donnees*/
    public static final String KEY_ROWID = "_id";
    public static final String KEY_CODE = "code";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_CATTYPE = "cat_type";
    private static final String DATABASE_TABLE = "category";
    private Context context;
    private SQLiteDatabase database;
    private DepenseDBHelper dbHelper;
    
    public CategoryDBAdapter(Context context) {
        this.context = context;
    }
    
    /** * ouvrir la base de donnees*/
    public CategoryDBAdapter open() throws SQLException {
        dbHelper = new DepenseDBHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }
    
    /** * fermer la base de donnees*/
    public void close() {
        dbHelper.close();
    }
    
    /** * cree une nouvelle categorie
         * si cette categorie est cree, la valeur de retour est le numero de la ligne: RowId  
         * sinon la valeur de retour sera -1 */
    public long createTodo(String CatType, String code, String description) {
        ContentValues initialValues = createContentValues(CatType, code,    description);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }
    
    /** * Mettre a jour une categorie */
    public boolean updateTodo(long rowId, String CatType, String code, String description) {
        ContentValues updateValues = createContentValues(CatType, code, description);

        return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
                + rowId, null) > 0;
    }
    
    /** * Supprimer une categorie */
    public boolean deleteTodo(long rowId) {
        return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
    
    /** * Lire la liste des categories*/
    public Cursor fetchAllTodos() {
        return database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_CATTYPE,
                KEY_CODE, KEY_DESCRIPTION }, null, null, null,
                null, KEY_CODE);
    }
    
    
    /** * Lire la categorie a une certaine ligne "RowId"*/
    public Cursor fetchTodo(long rowId) throws SQLException {
        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATTYPE, KEY_CODE, KEY_DESCRIPTION },
                KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    
    /** * mettre les parametres dans les colonnes convenables*/
    private ContentValues createContentValues(String CatType, String code, String description) {
        ContentValues values = new ContentValues();
        values.put(KEY_CATTYPE, CatType);
        values.put(KEY_CODE, code);
        values.put(KEY_DESCRIPTION, description);
        return values;
    }
}

